$(function() {
	init_rg("echarts1");//指标达成率【认购】
	init_qy("echarts2");//指标达成率【签约】
	init_hk("echarts3");//指标达成率【回款】
	init_ljxs("echarts4");//累计销售金额
	init_wsk("echarts5");//未收款预警
});

/**
 * 指标达成率【认购】
 */
function init_rg(id) {
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/cityReachRateSubscribeInfo",
		data: {
			merchantId: merchantId
		},
		dataType: 'json',
		success: function(res) {
			var files_cityName = new Array();
			var files_yearNum = new Array();
			var files_monthNum = new Array();
			var files_reachRateNum = new Array();
			var value = res;
			for(var i in value){
				files_cityName[i]=value[i].cityName;
				files_yearNum[i]=value[i].yearNum;
				files_monthNum[i]=value[i].monthNum;
				files_reachRateNum[i]=value[i].reachRateNum;
			}
			option_rg(id,files_cityName,files_yearNum,files_monthNum,files_reachRateNum);
		},
		error: function(res) {}
	});
	
	function option_rg(id,cityName,yearNum,monthNum,reachRateNum){
		var myChart = echarts.init(document.getElementById("" + id + ""));
		option = {
			title: {
				//text: '各城市公司指标达成率【年度、月度】 （认购）',
				left: 'left'
			},
			tooltip: {
				trigger: 'axis'
			},
			itemStyle: {
	            normal: {color: '#bd3b36'}
	        },
			grid: {
				top:'10px',
		        left: '0',
		        right: '0',
		        bottom: '0',
		        containLabel: true
		    },
			toolbox: {
				feature: {
					//dataView: {show: true, readOnly: false},
					//magicType: {show: true, type: ['line', 'bar']},
					//restore: {show: true},
					//saveAsImage: {show: true}
				}
			},
			legend: {
				//data:['年度金额','月度金额','达成率']
			},
			xAxis: [{
				type: 'category',
				data: cityName
			}],
			yAxis: [{
				/*type: 'value',
				//name: '水量',
				min: 0,
				max: 250,
				interval: 50,
				axisLabel: {
					formatter: '{value}'
				}*/
			}, {
				/*type: 'value',
				//name: '温度',
				min: 0,
				max: 25,
				interval: 5,
				axisLabel: {
					formatter: '{value}'
				}*/
			}],
			series: [{
				name: '年度金额',
				type: 'bar',
				itemStyle: {
		            normal: {color: '#ffcc99'}
		        },
				barWidth : 20,
				data: yearNum
			}, {
				name: '月度金额',
				type: 'bar',
				data: monthNum
			}, {
				name: '达成率',
				type: 'line',
				yAxisIndex: 1,
				data: reachRateNum
			}]
		};
		myChart.setOption(option);
	}
}
/**
 * 指标达成率【签约】
 */
function init_qy(id) {
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/cityReachRateSignInfo",
		data: {
			merchantId: merchantId
		},
		dataType: 'json',
		success: function(res) {
			var files_cityName = new Array();
			var files_yearNum = new Array();
			var files_monthNum = new Array();
			var files_reachRateNum = new Array();
			var value = res;
			for(var i in value){
				files_cityName[i]=value[i].cityName;
				files_yearNum[i]=value[i].yearNum;
				files_monthNum[i]=value[i].monthNum;
				files_reachRateNum[i]=value[i].reachRateNum;
			}
			option_qy(id,files_cityName,files_yearNum,files_monthNum,files_reachRateNum);
		},
		error: function(res) {}
	});
	
	function option_qy(id,cityName,yearNum,monthNum,reachRateNum){
		var myChart = echarts.init(document.getElementById("" + id + ""));
		option = {
			title: {
				//text: '各城市公司指标达成率【年度、月度】 （签约）',
				left: 'left'
			},
			tooltip: {
				trigger: 'axis'
			},
			itemStyle: {
	            normal: {color: '#157380'}
	        },
			grid: {
				top:'10px',
		        left: '0',
		        right: '0',
		        bottom: '0',
		        containLabel: true
		    },
			toolbox: {
				feature: {
					//dataView: {show: true, readOnly: false},
					//magicType: {show: true, type: ['line', 'bar']},
					//restore: {show: true},
					//saveAsImage: {show: true}
				}
			},
			legend: {
				//data:['年度金额','月度金额','达成率']
			},
			xAxis: [{
				type: 'category',
				data: cityName
			}],
			yAxis: [{
				/*type: 'value',
				//name: '水量',
				min: 0,
				max: 250,
				interval: 50,
				axisLabel: {
					formatter: '{value}'
				}*/
			}, {
				/*type: 'value',
				//name: '温度',
				min: 0,
				max: 25,
				interval: 5,
				axisLabel: {
					formatter: '{value}'
				}*/
			}],
			series: [{
				name: '年度金额',
				type: 'bar',
				itemStyle: {
		            normal: {color: '#a6cdd2'}
		        },
				barWidth : 20,
				data: yearNum
			}, {
				name: '月度金额',
				type: 'bar',
				data: monthNum
			}, {
				name: '达成率',
				type: 'line',
				yAxisIndex: 1,
				data: reachRateNum
			}]
		};
		myChart.setOption(option);
	}
}
/**
 * 指标达成率【回款】
 */
function init_hk(id) {
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/cityReachRatePaymentInfo",
		data: {
			merchantId: merchantId
		},
		dataType: 'json',
		success: function(res) {
			var files_cityName = new Array();
			var files_yearNum = new Array();
			var files_monthNum = new Array();
			var files_reachRateNum = new Array();
			var value = res;
			for(var i in value){
				files_cityName[i]=value[i].cityName;
				files_yearNum[i]=value[i].yearNum;
				files_monthNum[i]=value[i].monthNum;
				files_reachRateNum[i]=value[i].reachRateNum;
			}
			option_hk(id,files_cityName,files_yearNum,files_monthNum,files_reachRateNum);
		},
		error: function(res) {}
	});
	
	function option_hk(id,cityName,yearNum,monthNum,reachRateNum){
		var myChart = echarts.init(document.getElementById("" + id + ""));
		option = {
			title: {
				//text: '各城市公司指标达成率【年度、月度】 （回款）',
				left: 'left'
			},
			tooltip: {
				trigger: 'axis'
			},
			grid: {
				top:'10px',
		        left: '0',
		        right: '0',
		        bottom: '0',
		        containLabel: true
		    },
		    itemStyle: {
	            normal: {color: '#153f67'}
	        },
			toolbox: {
				feature: {
					//dataView: {show: true, readOnly: false},
					//magicType: {show: true, type: ['line', 'bar']},
					//restore: {show: true},
					//saveAsImage: {show: true}
				}
			},
			legend: {
				//data:['年度金额','月度金额','达成率']
			},
			xAxis: [{
				type: 'category',
				data: cityName
			}],
			yAxis: [{
				/*type: 'value',
				//name: '水量',
				min: 0,
				max: 250,
				interval: 50,
				axisLabel: {
					formatter: '{value}'
				}*/
			}, {
				/*type: 'value',
				//name: '温度',
				min: 0,
				max: 25,
				interval: 5,
				axisLabel: {
					formatter: '{value}'
				}*/
			}],
			series: [{
				name: '年度金额',
				type: 'bar',
				itemStyle: {
		            normal: {color: '#8abceb'}
		        },
				barWidth : 20,
				data: yearNum
			}, {
				name: '月度金额',
				type: 'bar',
				data: monthNum
			}, {
				name: '达成率',
				type: 'line',
				yAxisIndex: 1,
				data: reachRateNum
			}]
		};
		myChart.setOption(option);
	}
}
/**
 * 累计销售金额及占比
 */
function init_ljxs(id) {
	var months = $("#selectMonths").val();
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/eachFormatCumulativeInfo",
		data: {
			merchantId: merchantId,
			months: months
		},
		dataType: 'json',
		success: function(res) {
			var files_propertyType = new Array();
			var files_amount = new Array();
			var value = res;
			for(var i in value){
				files_propertyType[i]=value[i].propertyType;
				var rowAmount={};
				rowAmount.name=value[i].propertyType;
				rowAmount.value=value[i].amount;
				files_amount[i]=rowAmount;
			}
			option_ljxs(id,files_propertyType,files_amount);
		},
		error: function(res) {}
	});
	
	function option_ljxs(id,propertyType,amount){
		console.log(amount);
		var myChart = echarts.init(document.getElementById("" + id + ""));
		option = {
	    title : {
	        //text: '',
		    //subtext: '纯属虚构',
		    x:'left'
		},
		tooltip : {
		    trigger: 'item',
		    formatter: "{a} <br/>{b} : {c} ({d}%)"
		},
		legend: {
		    x : 'left',
		    y : 'bottom',
		    //data:['rose1','rose2','rose3']
		    data:propertyType
		},
		toolbox: {
		    show : true,
		    feature : {
		        /*mark : {show: true},
		        dataView : {show: true, readOnly: false},
		        magicType : {
		            show: true,
		            type: ['pie', 'funnel']
		        },
		        restore : {show: true},
		        saveAsImage : {show: true}*/
		    }
		},
		calculable : true,
		series : [
		    {
		        name:'销售金额及占比',
		        type:'pie',
		        radius : [20, 110],
		        center : ['50%', '50%'],
		        roseType : 'radius',
		        /*label: {
		            normal: {
		                show: false
		            },
		            emphasis: {
		                show: true
		            }
		        },*/
		        lableLine: {
		            normal: {
		                show: false
		            },
		            emphasis: {
		                show: true
		            }
		        },
		        data:amount
		       /* data:[
		            {value:10, name:'rose1'},
		            {value:5, name:'rose2'},
		            {value:15, name:'rose3'}
		        ]*/
		   /*  },
		    {
		        name:'面积模式',
		        type:'pie',
		        radius : [30, 110],
		        center : ['75%', '50%'],
		        roseType : 'area',
		        data:amount
		       data:[
		            {value:10, name:'rose1'},
		            {value:5, name:'rose2'},
		            {value:15, name:'rose3'}
		        ]*/
		    }
		    ]
		};
		myChart.setOption(option);
	}
}
/**
 * 未收款预警
 */
function init_wsk(id) {
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/expectedEarlyWarningInfo",
		data: {
			merchantId: merchantId
		},
		dataType: 'json',
		success: function(res) {
			var files_typeName = new Array();
			var files_noPayNum = new Array();
			var value = res;
			for(var i in value){
				files_typeName[i]=value[i].type;
				files_noPayNum[i]=value[i].number;
			}
			option_ljxs(id,files_typeName,files_noPayNum);
		},
		error: function(res) {}
	});
	
	function option_ljxs(id,typeName,noPayNum){
		var myChart = echarts.init(document.getElementById("" + id + ""));
		option = {
		    title: {
		        text: '',
		        //subtext: '数据来自网络'
		    },
		    tooltip: {
		        trigger: 'axis',
		        axisPointer: {
		            type: 'shadow'
		        }
		    },
		    legend: {
		        //data: ['2011年', '2012年'],
				top:'20px'
		    },
		    grid: {
		    	top:'10px',
		        left: '0',
		        right: '10px',
		        bottom: '0',
		        containLabel: true
		    },
		    xAxis: {
		        type: 'value',
		        boundaryGap: [0, 0.01]
		    },
		    yAxis: {
		        type: 'category',
		        data: typeName
		    },
		    series: [
		        {
		            name: '数量',
		            type: 'bar',
		            itemStyle: {
			            normal: {color: '#f87242'}
			        },
		            barWidth : 50,
		            data: noPayNum
		        /*},
		        {
		            name: '2012年',
		            type: 'bar',
		            data: [19325, 23438]*/
		        }
		    ]
		};
		myChart.setOption(option);
	}
}